package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    private final IUserService userService;
    private final StringRedisTemplate stringRedisTemplate;
    public FollowServiceImpl(IUserService userService, StringRedisTemplate stringRedisTemplate) {
        this.userService = userService;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    /**
     * 关注 取关
     * @param followUserId
     * @param isFollow
     * @return
     */
    @Override
    public Result follow(Long followUserId, boolean isFollow) {
        //得到用户id
        Long userId = UserHolder.getUser().getId();
        String key = "follows:" + userId;
        if(isFollow){
            //关注
            Follow follow = new Follow();
            //当前登录用户id
            follow.setFollowUserId(followUserId);
            //关注用户的id
            follow.setUserId(userId);
            boolean success = save(follow);
            //关注成功添加到redis
            if(success){
                stringRedisTemplate.opsForSet().add(key,followUserId.toString());
            }
        }else{
            //取关
            boolean success = remove(new QueryWrapper<Follow>().eq("follow_user_id", followUserId).eq("user_id", userId));
            if(success){
                stringRedisTemplate.opsForSet().remove(key,followUserId.toString());
            }
        }

        return Result.ok();
    }

    /**
     * 是否关注
     * @param followUserId
     * @return
     */
    @Override
    public Result isFollow(Long followUserId) {
        //获取登录的用户id
        Long userId = UserHolder.getUser().getId();
        //查询数据库是否存在信息
        Integer count = query().eq("follow_user_id", followUserId).eq("user_id", userId).count();
        //存在 1关注 不存在0 没关注
        return Result.ok(count > 0);
    }
    //查询共同关注
    @Override
    public Result commonFollow(Long followUserId) {
        Long id = UserHolder.getUser().getId();
        String key1 = "follows:" + id;
        String key2 = "follows:" + followUserId;

        Set<String> commonSet = stringRedisTemplate.opsForSet().intersect(key1, key2);
        if(commonSet.isEmpty()||commonSet==null){
            return Result.ok(Collections.emptyList());
        }
        List<Long> commonList = commonSet.stream().map(Long::valueOf).collect(Collectors.toList());
        List<UserDTO> userDTOS = userService.listByIds(commonList)
                .stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        return Result.ok(userDTOS);
    }
}
